package com.android.functionmanagement.location;

import com.android.functionmanagement.globle.LogGloble;
import com.android.functionmanagement.util.DeviceUtils;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class GPSLocationMode extends LocationMode {

	protected static final String TAG = "GPSLocationMode";
	private LocationManager mLocationManager;
	boolean isOpenGPS = false;
	
	Handler handler =new Handler();

	@Override
	public void init(Context context) {
		this.mContext = context;
		mLocationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if(mLocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER)){
			isOpenGPS = true;
		}else{
			isOpenGPS = false;
			DeviceUtils.toggleGPS(context, true);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		handler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				reset();
				
			}
		}, 5*60*1000);
		// 为获取地理位置信息时设置查询条件
		String bestProvider = mLocationManager.getBestProvider(getCriteria(),
				true);
		// 获取位置信息
		// 如果不设置查询要求，getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
		//Location location = mLocationManager.getLastKnownLocation(bestProvider);

		// 1秒更新一次，或最小位移变化超过1米更新一次；
		// 注意：此处更新准确度非常低，推荐在service里面启动一个Thread，在run中sleep(10000);然后执行handler.sendMessage(),更新位置
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1,
				new LocationListener() {
					
					@Override
					public void onStatusChanged(String arg0, int status, Bundle arg2) {
						 switch (status) {
				            //GPS状态为可见时
				            case LocationProvider.AVAILABLE:
				                LogGloble.i(TAG, "当前GPS状态为可见状态");
				                Toast.makeText(mContext, "当前GPS状态为服务区外状态", 4000).show();
				                break;
				            //GPS状态为服务区外时
				            case LocationProvider.OUT_OF_SERVICE:
				            	LogGloble.i(TAG, "当前GPS状态为服务区外状态");
				            	Toast.makeText(mContext, "当前GPS状态为服务区外状态", 4000).show();
				                break;
				            //GPS状态为暂停服务时
				            case LocationProvider.TEMPORARILY_UNAVAILABLE:
				            	LogGloble.i(TAG, "当前GPS状态为暂停服务状态");
				            	Toast.makeText(mContext, "当前GPS状态为暂停服务状态", 4000).show();
				            	if(mCompleteLister!=null){
									mCompleteLister.onCompleted(null, ERR);
									mLocationManager.removeUpdates(this);
									reset();
				            	}
				                break;
				            }
					}
					
					/**
			         * GPS开启时触发
			         */
					@Override
					public void onProviderEnabled(String arg0) {
						
					}
					
					/**
			         * GPS禁用时触发
			         */
					@Override
					public void onProviderDisabled(String arg0) {
						
					}
					
					@Override
					public void onLocationChanged(Location data) {
						Toast.makeText(mContext, "onLocationChanged.....", 4000).show();
						if(mCompleteLister!=null && data!=null){
							mCompleteLister.onCompleted(data, SUCCESS);
							reset();
						}else if(mCompleteLister!=null){
							mCompleteLister.onCompleted(null, ERR);
							reset();
						}
						mLocationManager.removeUpdates(this);
					}
				});

	}

	@Override
	public Object getLocationInfo() {
		return null;
	}

	/**
	 * 返回查询条件
	 * 
	 * @return
	 */
	private Criteria getCriteria() {
		Criteria criteria = new Criteria();
		// 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略，Criteria.ACCURACY_FINE则比较精细
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		// 设置是否要求速度
		criteria.setSpeedRequired(false);
		// 设置是否允许运营商收费
		criteria.setCostAllowed(false);
		// 设置是否需要方位信息
		criteria.setBearingRequired(false);
		// 设置是否需要海拔信息
		criteria.setAltitudeRequired(false);
		// 设置对电源的需求
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		return criteria;
	}

	@Override
	public void setLocationCompleteLister(LocationCompleteLister completeLister) {
		this.mCompleteLister = completeLister;
	}
	
	void reset(){
		DeviceUtils.toggleGPS(mContext, isOpenGPS);
	}

}
